<template>
	<view>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20" >
			<view class="content wrap">
				<u-form>
					<u-form-item label="配送日期" label-width="170" left-icon="calendar"><u-input v-model="keyword" placeholder="选择日期" type="select" @click="getT()"/></u-form-item>
				</u-form>
			</view>
		</view>
		<u-card padding="16" v-for="(item,index) in datalist" :key="index">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view class="weikaishi">
								<span>收货门店：{{item.clientName}}</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<view>
						<u-row>
							<u-col span="12" text-align="left">
								<view class=" u-m-t-5 huanhang lanse">
									<span>排车时间：{{item.createTime}}</span>
								</view>
							</u-col>
						</u-row>
						<view v-for="(item2,index1) in item.roadWays" :key="index1">
							<u-row>
								<u-col span="6" text-align="left">
									<view class=" u-m-t-5">
										<span>巷道号：{{item2.roadWayCode}}</span>
									</view>
								</u-col>
								<u-col span="6" text-align="left">
									<view class=" u-m-t-5">
										<span>平台编号：{{item2.dockPlat}}</span>
									</view>
								</u-col>
							</u-row>
							<u-row>
								<u-col span="6" text-align="left">
									<view class=" u-m-t-5">
										<span>已有托数：{{item2.seq}}</span>
									</view>
								</u-col>
							</u-row>
						</view>
					</view>
				</view>
			</view>
		</u-card>
		<view v-if="btShow">
			<u-button type="success"  @click="queren()">确认</u-button>
			<u-button style="margin-top: 15rpx; margin-bottom: 15rpx;" type="error" @click="quxiao()">拒绝</u-button>
		</view>
		<view v-if="!btShow">
			<u-button style="margin-top: 15rpx; margin-bottom: 15rpx;" type="primary" @click="ksdc()">开始到仓</u-button>
		</view>
		<view>
			<u-calendar v-model="show" mode="date" max-date="2070-01-01" @change="change"></u-calendar>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import config from '../../../config.js'
	export default {
		data() {
			return {
				flex:'',
				keyword:"",
				btShow:true,
			    pageNo: 1,
			    hasMore: true,
			    orderCount: 0,
				datalist:[],
				show:false,
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			
			let year = new Date().getFullYear();//年
			let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;
			let day = new Date().getDate();//日
			this.keyword =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			
			this.refresh()
		},
		onPullDownRefresh() {
			this.refresh()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		methods: {
			change(e) {
				this.keyword = e.result
				this.refresh()
			},
			getT(){
				this.show = true
			},
			queren(){
				uni.showLoading({
					title: '正在提交...'
				})
				this.$http.post('/rest/transport/queren',{
				},{
					params: {
						// id:item.id
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						this.$refs.uToast.show({
							title: '操作成功',
							type: 'success'
						})
						setTimeout(() => {
							this.refresh();
						}, 500)
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
						setTimeout(() => {
							this.refresh()
						}, 500)
					}
				})
			},
			quxiao(){
				uni.showModal({
					content: '确定要拒绝吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交...'
							})
							this.$http.post('/rest/transport/quxiao',{
							},{
								params: {
									// id:item.id
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									setTimeout(() => {
										this.refresh();
									}, 500)
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
									setTimeout(() => {
										this.refresh()
									}, 500)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh(){
				uni.showLoading({
					title: '加载中'
				})
				this.$http.post('/rest/transport/findorderOutpreparePlatooncar',{
				},{
					params: {
						keyword:this.keyword,
						statNotIn:'已拒绝'
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					this.datalist = res.data.data
					if(this.datalist.length > 0 && this.datalist[0].stat == '已确认'){
						this.btShow = false;
					}
				})
			},
			search(e){
				this.refresh()
			},
			ksdc(e){
				console.log(e);
				console.log(this.datalist)
				let inwarehousTrack =
				{
					longitude : '',
					latitude : '',
					clientVolSumId: this.datalist[0].clientVolSumId,
					carId:this.datalist[0].carId,
					carnumber:this.datalist[0].carnumber,
					estDeliveryDate:this.datalist[0].estDeliveryDate,
					siji:this.datalist[0].sijidianhua,
					createTime:new Date()
					
				}
		
				wx.startLocationUpdateBackground({
					success: (res) => {
						const intervalId  = setInterval(() => {
							uni.getLocation({
								type: 'gcj02',
								altitude: false,
								success: function(res) {
									// 创建新的定位点
									console.log("创建新的定位点")
									console.log(res)
									inwarehousTrack.latitude = res.latitude
									inwarehousTrack.longitude = res.longitude
									uni.request({
										method: "POST",
										url: config.root +'/rest/inwarehouseTrack/uploadPoint',
										data: inwarehousTrack,
										header: {
											'Authorization': uni.getStorageSync('token'),
										},
										success: (res) => {
											console.log("上传轨迹点成功")
											console.log(res)
											// 上传成功后清空数组											
										}
									})
								},
								fail: function(res) {
									// console.log("获取当前位置失败")
								},
							})
						}, 30000)
						
						uni.setStorageSync('intervalId', intervalId);
						uni.setStorageSync('carnumber', inwarehousTrack.carnumber);
					},
					fail: (res) => {
						const msg = '开启持续定位失败。请在我的-定位授权中开启使用小程序期间和离开小程序后位置权限';
						uni.showToast({
							icon: 'none',
							title: msg,
							duration: 4000
						});
					}
				})
				
			}
		}
	}
</script>
<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}
	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}
	.weikaishi2{
		font-size: 34rpx;
	}
	.baise{
		color: #ffffff;
		font-size: 34rpx;
	}
	.weikaishi3{
		font-size: 30rpx;
	}
	.baise{
		background-color: #2979ff;
		color: #ffffff;
		font-size: 34rpx;
	}
	.huanhang{
		word-break:break-all;
	}
	.margin-zdy{
		padding: 5rpx 20rpx 5rpx 30rpx;
		background-color: #FFFFFF;
	}
	.foot{
		display: flex;
		font-size: 34rpx;
		justify-content: center;
		padding-left: 30rpx;
		padding-right: 30rpx;
	}
</style>
